﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

using Kooboo.Web.Mvc;
using Kooboo.CMS.Content.Models;

namespace Kooboo.CMS.DataModule.Models
{
    public class FoldersDataSource : Kooboo.Web.Mvc.ISelectListDataSource
    {
        public IEnumerable<System.Web.Mvc.SelectListItem> GetSelectListItems(System.Web.Routing.RequestContext requestContext, string filter = null)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            foreach(var folder in Kooboo.CMS.Content.Services.ServiceFactory.TextFolderManager.All(Repository.Current, ""))
            {
                ChildFolders(ref list, folder);
            }
            return list;
        }

        private void ChildFolders(ref List<SelectListItem> list, TextFolder folder, string prefix = "")
        {
            list.Add(new SelectListItem() { Text = prefix + folder.FriendlyText, Value = folder.FullName });
            foreach(var child in Kooboo.CMS.Content.Services.ServiceFactory.TextFolderManager.ChildFolders(folder, ""))
            {
                ChildFolders(ref list, child, prefix + HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;"));
            }
        }
    }

    public class ExportModel
    {
        [UIHint("DropDownList")]
        [DataSource(typeof(FoldersDataSource))]
        public string FolderName { get; set; }
    }
}